using UnityEngine;

public class carCamera : MonoBehaviour
{
	public car car1;

	public Transform target;

	public float height = 1f;

	public float positionDamping = 3f;

	public float velocityDamping = 3f;

	public float distance = 4f;

	public LayerMask ignoreLayers = -1;

	private RaycastHit hit = default(RaycastHit);

	private Vector3 prevVelocity = Vector3.zero;

	private LayerMask raycastLayers = -1;

	private Vector3 currentVelocity = Vector3.zero;

	private void Start()
	{
		raycastLayers = ~(int)ignoreLayers;
	}

	private void Update()
	{
		if (car1.controlled)
		{
			Vector3 velocity = target.root.GetComponent<Rigidbody>().velocity;
			if ((double)target.root.GetComponent<Rigidbody>().velocity.magnitude < 0.01)
			{
				velocityDamping = 0f;
			}
			else
			{
				velocityDamping = 3f;
			}
			currentVelocity = Vector3.Lerp(prevVelocity, velocity, velocityDamping * Time.deltaTime);
			currentVelocity.y = 0f;
			prevVelocity = currentVelocity;
		}
	}

	private void LateUpdate()
	{
		if (car1.controlled && Camera.main != null)
		{
			float num = Mathf.Clamp01(target.root.GetComponent<Rigidbody>().velocity.magnitude / 60f);
			if (num < 0.01f)
			{
				num = 0.01f;
			}
			Camera.main.fieldOfView = Mathf.Lerp(40f, 65f, num);
			float num2 = Mathf.Lerp(7.5f, 6.5f, num);
			currentVelocity = currentVelocity.normalized;
			Vector3 vector = target.position + Vector3.up * height;
			Vector3 vector2 = vector - currentVelocity * num2;
			vector2.y = vector.y;
			Vector3 direction = vector2 - vector;
			if (Physics.Raycast(vector, direction, out hit, num2, raycastLayers))
			{
				vector2 = hit.point;
			}
			Camera.main.transform.position = vector2;
			Camera.main.transform.LookAt(vector);
		}
		else if (Camera.main == null)
		{
			UnityEngine.Debug.Log("please add a camera to the scene with tag 'MainCamera' ");
		}
	}
}
